/* UVa 11450 - Wedding Shopping
 * Dynamic Programming */
#include<bits/stdc++.h>
using namespace std;
int m, c;
vector<vector<int>> garments;
bool dp[26][206];
void reset() {
  memset(dp, 0, sizeof dp);
  garments.clear();
}
void solve() {
  for(auto el:garments[0])
    if(el<m)dp[0][el]=true;
  for(int i=1; i<c; ++i) {
    for(int j=0; j<=m; ++j) {
      if(dp[i-1][j]) {
        for(int el:garments[i]) {
          if(j+el <= m)
            dp[i][j+el]=true;
        }
      }
    }
  }
  for(int i=m; i>=0; --i) {
    if(dp[c-1][i]) {
      cout<<i<<'\n';
      return;
    }
  }
  cout<<"no solution\n";
}
int main() {
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  int tc;cin>>tc;
  while(tc--) {
    reset();
    cin>>m>>c;
    garments.resize(c);
    for(auto&row:garments) {
      int cnt;cin>>cnt;
      row.resize(cnt);
      for(auto&el:row)cin>>el;
    }
    solve();
  }
}
